# GCC路径
GCC_PATH = /usr/bin
PWD=`pwd`

# 目标名字
TARGET = nuc980

BUILD_DIR = build

################# NUC980_HAL ###########################

NUC980_HAL ?= Driver

C_INCLUDES = \
    -I. \
    -I$(NUC980_HAL)/Include

C_SOURCES = \
    $(NUC980_HAL)/sys.c \
	$(NUC980_HAL)/uart.c \
	$(NUC980_HAL)/cache.c \
	$(NUC980_HAL)/adc.c \
	$(NUC980_HAL)/gpio.c \
	$(NUC980_HAL)/etimer.c \
	$(NUC980_HAL)/rtc.c \
	$(NUC980_HAL)/retarget.c \
	$(NUC980_HAL)/wdt.c

################### FreeRTOS #############################

FreeRTOS ?= ../../FreeRTOS
C_INCLUDES += \
    -I$(FreeRTOS)/include \
    -I$(FreeRTOS)/portable/GCC/NUC980 \
    -I$(FreeRTOS)

C_SOURCES +=  \
    $(FreeRTOS)/list.c \
    $(FreeRTOS)/queue.c \
    $(FreeRTOS)/tasks.c \
    $(FreeRTOS)/timers.c \
    $(FreeRTOS)/event_groups.c \
    $(FreeRTOS)/portable/MemMang/heap_4.c \
    $(FreeRTOS)/portable/GCC/NUC980/port.c \
    $(FreeRTOS)/portable/GCC/NUC980/portISR.c

################### IEC-60730 #############################

IEC_60730 ?= IEC-60730
C_INCLUDES += \
    -I$(IEC_60730)

C_SOURCES +=  \
    $(IEC_60730)/adc_mux_test_runtime.c \
    $(IEC_60730)/CLASSB_RUNTIME_TESTS.c \
    $(IEC_60730)/CLASSB_STARTUP_TESTS.c \
    $(IEC_60730)/CLASSB_TESTLIB_INIT.c \
    $(IEC_60730)/dram_marchcx_test_runtime.c \
    $(IEC_60730)/flash_test_runtime.c \
    $(IEC_60730)/flash_test_startup.c \
    $(IEC_60730)/global_var_backup.c \
    $(IEC_60730)/global_var.c \
    $(IEC_60730)/gpio_test_startup.c \
    $(IEC_60730)/IEC60730_SAFETY_TEST_LIB.c \
    $(IEC_60730)/interrupt_test_runtime.c \
    $(IEC_60730)/interrupt_test_startup.c \
    $(IEC_60730)/pc_test_runtime.c \
    $(IEC_60730)/pc_test_startup.c \
    $(IEC_60730)/ram_march_test.c \
    $(IEC_60730)/runtime_test.c \
	$(IEC_60730)/stack_overrun_ptrn.c \
    $(IEC_60730)/timer_test_startup.c \
	$(IEC_60730)/stack_test_runtime.c \
    $(IEC_60730)/uart_test.c

C_SOURCES += \
    main.c \
	nostdlib.c\
	printf.c \
    task_main.c

############################ Boot #######################

# ASM sources
ASM_SOURCES =  \
    $(IEC_60730)/cpureg_test_runtime.s \
    $(IEC_60730)/cpureg_test_startup.s \
    $(IEC_60730)/dram_machx_wom_test_startup.s \
    $(IEC_60730)/dram_marchc_wom_test_runtime.s \
    $(IEC_60730)/dram_marchc_wom_test_startup.s \
    $(IEC_60730)/dram_marchx_wom_test_runtime.s \
    $(IEC_60730)/ram_marchc_wom_test_runtime.s \
	$(IEC_60730)/ram_marchc_wom_test_startup.s \
	$(IEC_60730)/ram_marchx_wom_test_runtime.s \
	$(IEC_60730)/ram_marchx_wom_test_startup.s \
	startup.s

######################### build flags ###################
# debug build?
DEBUG = 1
# optimization
OPT =  -Og
PREFIX = arm-none-eabi-
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
AS_DEFS =

# compile gcc flags
# -fdata-sections -ffunction-sections:
# 编译时单独创建函数为section，在链接的阶段使用 -Wl, -gc-sections 去掉没有用到的sections, 能够大量减少文件体积
ASFLAGS = -mcpu=arm926ej-s $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = -mcpu=arm926ej-s $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS += -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -Wno-unused-but-set-variable
# CFLAGS += -mfpu=fpv4-sp-d16 -mfloat-abi=softfp

# 生成 gdb 调试信息 格式为 dward-2
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif

# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
# link script
LDSCRIPT = nuc980.ld

# c 库、数学库、nosys库
LIBS = -lc -lm -lnosys
LIBDIR =
# -specs=nano.specs： 链接时使用newlib(libc)减少大量文件体积
# -Wl,-Map=xxxxxxxx.map:生成链接映射列表
# --cref: 输出交叉引用列表
# -gc-sections: 去掉没有用到的sections, 能够大量减少文件体积
LDFLAGS = -mcpu=arm926ej-s -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin


# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@

$(BUILD_DIR):
	mkdir $@


clean:
	-rm -fR $(BUILD_DIR)

-include $(wildcard $(BUILD_DIR)/*.d)
